package pta09;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UDPServer {

	public static void main(String[] args) throws IOException {
			DatagramSocket socket = new DatagramSocket(9999);
			byte[] buf = new byte[256];//201521123013
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            while (true) {
                socket.receive(packet);
                String data = new String(packet.getData()).trim();
                if(data.trim().equals("1")){
    				Date time=new Date();
    				String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    							.format(Calendar.getInstance().getTime())+"\n";
    				packet.setData(date.getBytes());
    				socket.send(packet);
    			}
    			if(data.trim().equals("2")){
    				String ip=packet.getSocketAddress().toString();
    				packet.setData(ip.getBytes());
    				socket.send(packet);
    			}
    			if(data.trim().equals("3")){
    				
    			}
    			if(data.trim().equals("4")){
    				break;
    			}
            }
        }
}
